package gui;

import client.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import msg.MsgContent;


/**
 *
 * @author  tomek
 */
public class ConversationGui extends javax.swing.JFrame implements WindowListener {
    
    private Conversation myConversation;
    private String u;
    private Connection c;
    private Vector<String> msgs;
    private Calendar cal;
    private Action enterAction;
    
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JScrollPane jScrollPane2;
    private JScrollPane jScrollPane3;
    private JTabbedPane jTabbedPane1;
    private JTextArea mainTextArea;
    private JTextArea bottomTextArea;
    private JTextArea upperTextArea;
    
    public ConversationGui(String who, client.Conversation conv) {
        myConversation = conv;
        c = new Connection(1027);
        u = who;
        msgs = new Vector<String>();
        cal = Calendar.getInstance();
        enterAction = new EnterAction();
        
        this.setTitle(u);
        initComponents();
        
        
        
/* DO TESTÓW
 
    
 
 */        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        
        
        
        jPanel1 = new JPanel();
        jScrollPane1 = new JScrollPane();
        mainTextArea = new JTextArea();
        jScrollPane2 = new JScrollPane();
        bottomTextArea = new JTextArea();
        jTabbedPane1 = new JTabbedPane();
        jScrollPane3 = new JScrollPane();
        upperTextArea = new JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(this);

        bottomTextArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "pressed");
        bottomTextArea.getActionMap().put("pressed", enterAction);
        
        mainTextArea.setColumns(20);
        mainTextArea.setEditable(false);
        mainTextArea.setLineWrap(true);
        mainTextArea.setRows(5);
        
        jScrollPane1.setViewportView(mainTextArea);
        

        bottomTextArea.setColumns(20);
        bottomTextArea.setRows(1);
        bottomTextArea.setWrapStyleWord(true);
        jScrollPane2.setViewportView(bottomTextArea);

        jTabbedPane1.setOpaque(true);

        upperTextArea.setColumns(20);
        upperTextArea.setEditable(false);
        upperTextArea.setRows(1);
        upperTextArea.setText(u);
        jScrollPane3.setViewportView(upperTextArea);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        
    }// </editor-fold>
    
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new ConversationGui("AA").setVisible(true);
//            }
//        });
//    }
    
    
    
    public void showGui() {
        pack();
        setVisible(true);
    }
    
    public void addMsg(msg.MsgContent m) {
        Locale currentLocale = new Locale("de", "DE");
        DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                                   DateFormat.SHORT, currentLocale);
        
        //System.out.printl
        String tmp = "["+formatter.format(m.getDate())+"] " +"<"+m.getSender()+"> "+m.getContent()+"\n";
        msgs.add(tmp);
        mainTextArea.append(tmp);
        
    }
    
    class EnterAction extends AbstractAction {
        String txt;
        public EnterAction() {
            
        }
        public void actionPerformed(ActionEvent e) {
            if (!(txt = bottomTextArea.getText()).equals("")) {
                MsgContent ms = new MsgContent(Client.getLogin(), u, cal.getTimeInMillis(), txt);
                addMsg(ms);
                bottomTextArea.setText("");
                c.sendMsg(ms);
            }
        }   

        
    }
    
        //WindowListener methods
    public void windowActivated(WindowEvent e) {}
    public void	windowClosed(WindowEvent e) {}
    public void	windowClosing(WindowEvent e) {
        System.out.println("Zamykam");
        
        this.setVisible(false);
  

    }
    public void	windowDeactivated(WindowEvent e) {}
    public void	windowDeiconified(WindowEvent e) {}
    public void	windowIconified(WindowEvent e) {}
    public void	windowOpened(WindowEvent e) {}
    
}
